﻿@model YouWei.Infrastructure.Entity.LiveRoom
@{
    ViewBag.Title = "直播室管理";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section style{ 
    <link href="@Url.Content("~/lib/select2/css/select2.min.css")" rel="stylesheet">
    <link href="@Url.Content("~/lib/bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css")" rel="stylesheet">
}
<section class="content-header">
    <h1>
        直播室管理
        <small></small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i>主页</a></li>
        <li><a href="#" id="preBtn">直播室管理列表</a></li>
        <li class="active">直播室管理</li>
    </ol>
</section>
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="box box-primary">
                <div class="box-header">
                </div>
                <!-- /.box-header -->
                <div class="box-body table-responsive no-padding">
                    <form class="form-horizontal" id="form1">
                        <div class="box-body">
                            <input type="hidden" name="hidID" id="hidID" value="@Model.RoomID" />
                            <div class="form-group">
                                <label class="col-sm-2 control-label">名称</label>
                                <div class="col-sm-3">
                                    <input type="text" name="txtName" id="txtName" value="@Model.Name" class="form-control" maxlength="50" autocomplete="off">
                                </div>
                            </div>
                            <div class="form-group" id="SelCarBrand">
                                <label class="col-sm-2 control-label">主播</label>
                                <div class="col-sm-3" id="SelBrandDiv">
                                    <select id="selUser" name="selUser" class="form-control">
                                        <option value="0">请选择</option>
                                    </select>

                                </div>                               
                            </div>
                            
                            <div class="form-group">
                                <label class="col-sm-2 control-label">封面图</label>
                                <div class="col-sm-3">
                                    <div id="HeadUrl" class="dm-uploader">
                                        <div role="button" class="btn btn-primary mr-2">
                                            <i class="fa fa-fw fa-folder-o"></i>图片上传
                                            <input type="file" title="Click to add Files">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label"></label>
                                <div class="col-sm-3">
                                    <div class="over-hidden" style="">
                                        @if (Model.RoomID > 0)
                                        {
                                            <div id="divImg" class="img-wrapper-titlephoto fl" style="display: block;">
                                                <img id="imgPhoto" style="width: 360px; height: 160px;border:1px solid #ccc " src="@string.Format(" {0}/{1}", YouWei.Utility.ConfigHelper.GetAppSettingValue("CommentImageServerUrl"), Model.CoverImage)" />
                                                <input type="hidden" id="hidDbUrl" value="@Model.CoverImage" />
                                            </div>
                                        }
                                        else
                                        {
                                            <div id="divImg" class="img-wrapper-titlephoto fl" style="display: none;">
                                                <img id="imgPhoto" style="width: 360px; height: 160px;border:1px solid #ccc " src="" />
                                                <input type="hidden" id="hidDbUrl" />

                                            </div>
                                        }
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">室内人数</label>
                                <div class="col-sm-3">
                                    <input type="text" name="txtMaxPerson" id="txtMaxPerson" value="@Model.MaxPerson" class="form-control" maxlength="10">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">管理员微信号</label>
                                <div class="col-sm-3">
                                    <input type="text" name="txtManager" id="txtManager" value="@Model.OpenID" class="form-control" maxlength="20" autocomplete="off">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">开始时间</label>
                                <div class="col-sm-3">
                                    <input type="text" name="txtStartTime" id="txtStartTime" value="@Model.StartTime" class="form-control" autocomplete="off">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">直播时长</label>
                                <div class="col-sm-3">
                                    <input type="text" name="txtDuration" id="txtDuration" value="@Model.Duration" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">描述</label>
                                <div class="col-sm-3">
                                    <textarea name="txtDescription" id="txtDescription" class="form-control" maxlength="200">@Model.Description</textarea>
                                </div>

                            </div>
                            <!-- /.box-body -->
                            <div class="box-footer text-center">
                                <button class="btn btn-info" type="button" id="btnSave">保存</button>
                            </div>
                            <!-- /.box-footer -->
                        </div>
                    </form>
                </div>
                <div class="box-footer clearfix ">
                </div>
                <!-- /.box-body -->
                <!-- /.box -->
            </div>
        </div>
    </div>
</section>
@section scriptsBottom{
    <script type="text/javascript" src="@Url.Content("~/lib/select2/js/select2.min.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/lib/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/lib/bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js")"></script>
    <script type="text/javascript">
    var userId = 0; //是否是添加页面，如果是编辑页则判断条件增加ID
    $(document).ready(function () {
        renderImg();
        initFormValidate();
        binds();
        initUser();
        $("#txtStartTime").datetimepicker({
            format: 'yyyy-mm-dd hh:ii',
            language:'zh-CN',
            autoclose: 1,//选择后自动关闭
            todayHighlight: 1,//今天高亮
            startView: 2,
            forceParse: 0,
            startDate:'@DateTime.Now.ToString("yyyy-MM-dd")'
        });

    });

    //保存
    function save() {
        var bootstrapValidator = $("#form1").data('bootstrapValidator');
        bootstrapValidator.validate();
        if (bootstrapValidator.isValid()) {
            var url = '@Url.Action("SaveLiveRoom", "Live")';
            var options = {
                url: url,
                type: "POST",
                data: {
                    RoomID: $("#hidID").val(),
                    Name: $("#txtName").val(),
                    Description: $("#txtDescription").val(),
                    CoverImage: $("#hidDbUrl").val(),
                    UserID: $("#selUser").val(),
                    StartTime: $("#txtStartTime").val(),
                    OpenID: $("#txtManager").val(),
                    MaxPerson: $("#txtMaxPerson").val(),
                    Duration: $("#txtDuration").val()
                },
                success: function (o) {
                    if (o.status == 0) {
                        BMCW.msg("保存成功", function () {
                            location.href='@Url.Action("Index", "Live")';
                        });
                    } else {
                        BMCW.alert(o.message);
                    }
                }
            };

            BMCW.ajax(options);
        }
    }
    function binds() {
        //保存
        $("#btnSave").click(function () {
            save();
        });
        $("#preBtn").click(function () {
            var url = '@Url.Action("Index", "Live")';
            location.href = url;
        });
       
    }

    function initFormValidate() {
        $("#form1").bootstrapValidator({
            submitHandler: function (valiadtor, loginForm, submitButton) {
                valiadtor.defaultSubmit();
            },
            fields: {
                txtName: {
                    validators: {
                        notEmpty: {
                            message: '请输入名称'
                        }
                    }
                },
                selUser: {
                    validators: {
                        notEmpty: {
                            message: '请选择主播'
                        }
                    }
                },
                txtStartTime: {
                    validators: {
                        notEmpty: {
                            message: '请输入开始时间'
                        }
                    }
                },
                txtMaxPerson: {
                    validators: {
                        notEmpty: {
                            message: '请输入室内人数'
                        },
                        numeric: {
                            message: '数量只能输入数字'
                        }
                    }
                },
                txtDuration: {
                    validators: {
                        notEmpty: {
                            message: '请输入直播时长'
                        },
                        numeric: {
                            message: '数量只能输入数字'
                        }
                    }
                },
                txtDescription:{
                    stringLength : {
                        max : 2000,
                        message : '不超过2000个字符'
                    }
                }
            }
        });
    }

    function initUser()
    {
        var uid = @Model.UserID;
        
        var url = '@Url.Action("GetAllUserList", "User")';
        BMCW.ajax({
            url: url,
            type: "GET",
            success: function (o) {
                if (o.length > 0) {
                    var sdata = []
                    for (var i = 0; i < o.length; i++) {
                        var usr =  {
                            id: o[i].UserID,
                            text: ''+o[i].UserName+''
                        };
                        sdata.push(usr)

                    }
                    $('#selUser').select2({
                        data: sdata,
                        minimumResultsForSearch: 1
                    });
                    if (uid>0) {
                        $("#selUser").val(uid).trigger("change");
                    }
                } else {
                    BMCW.alert(o.message);
                }
            }
        });
    }

    function renderImg() {
        $('#HeadUrl').dmUploader({ //
            url: '@Url.Action("UploadImg", "Product")',
            maxFileSize: 3000000, // 3 Megs max
            multiple: false,
            allowedTypes: 'image/*',
            extFilter: ['jpg', 'jpeg', 'png', 'gif'],
            onUploadSuccess: function (id, response) {
                if (response.status==0) {
                    var oData = response.data;
                    $("#imgPhoto").attr("src",oData.HttpImgUrl);
                    $("#hidDbUrl").val(oData.ImgUrl);//存数据库里的相对路径
                    $("#divImg").show();
                  //  $("#hidImgPhoto-error").remove();
                } else {
                    alert(response.data.message);
                }
            }
        });

    }

    </script>
}